// pages/components/page_display/collocation/collocation.js
var request_api = require('../../../../utils/require_api.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    is_downloaded: { //是否已经下载
      type: Array,
      value: []
    },
    is_all_downloads: {  //是否必下载
      type: Array,
      value: []
    },
    showdapei: { 
      type: String,
      value:''
    },
    image_url: {  //图片地址
      type: Array,
      value:[]
    },
    downloads:{  //下载次数
      type:Array,
      value:[]
    },
    content: {  //推广的内容
      type:Array,
      value:[],
      observer: function (a, r, e) {}
    },
    video_url: {  //视频的地址
      type: String,
      value: ''
    },
    buttonTop: {  //视频的上位置
      type: Number,
      value: 0
    },
    buttonLeft: { //视频的左位置
      type: Number,
      value: 0
    },
    istrue: { //是否显示比下载人员的一下载和为下载dialog
      type:Boolean
    },
    ismanager:{ //是否是管理员
      type:Number,
      value:0
    }
  }, 
  /**
   * 组件的初始数据
   */
  data: {
    isnocontent:'暂无数据...',
    indicatorDots: true, //轮播图的下标
    autoplay: false, //是否自动播放
    circular: false, //是否循环
    swiperCurrent: 0, //当前所在滑块的 index
    beforeColor: "white", //指示点颜色  
    afterColor: "coral", //当前选中的指示点颜色  
    previousmargin: '0px', //前边距
    nextmargin: '0px', //后边距
    curindex: 0,
    viewDownloader: [],
    downloader: [],
    notdownloader: [], 
    istrue1:false,
    isdown:1
  },

  /**
   * 组件的方法列表
   */
  methods: {
    openDialog: function (event) {
      console.log(event,"this is event dataItem 74");
      this.get_downloadpeople(event.currentTarget.dataset.item[0].tuiguang_key);
      this.setData({
        istrue1: true,
        isdown: 1,
      })
    },
    closeDialog: function () {
      this.setData({
        istrue1: false
      })
    },
    //已下载人员
    downloaded: function () {
      this.data.viewDownloader = this.data.downloader;
      this.setData({
        isdown:1,
        viewDownloader: this.data.viewDownloader
      })
    },
    //未下载人员
    notDownloaded: function () {
      this.data.viewDownloader = this.data.notdownloader;
      this.setData({
        isdown: 0,
        viewDownloader: this.data.viewDownloader
      })
    },

    get_downloadpeople(key) {
      var that = this;
      that.data.downloader = [];
      that.data.notdownloader = [];
      request_api.request({
        "cmd": "weixin_api|get_downloads_people",
        "paras": [{
          name: "tuiguang_key",
          value: key
        },]
      }, //需要传的 data
        function (res) {
          console.log(res,"this is res at collocation.js 120");
          for (let i = 0; i < res.data.result.length; i++) {
            if (res.data.result[i].download_detail_key != 0) {
              that.data.downloader.push(res.data.result[i]);
            } else {
              that.data.notdownloader.push(res.data.result[i]);
            }
          }
          that.setData({
            viewDownloader: that.data.downloader
          })
        },
        function () {
          console.log("错误");
        },
      )
    },


    //预览搭配图片
    previewImage1: function(e) {
      if (e.target.dataset.pic_url[0].poster) {

      } else {
        var url_image = [];
        var current = '';
        for (let i = 0; i < e.target.dataset.pic_url.length; i++) {
          url_image.push(e.target.dataset.pic_url[i].url.split("~")[0]); //url + '?' + Math.random() / 9999;
        }
        current = url_image[e.target.dataset.index];
        wx.previewImage({
          current: current, // 当前显示图片的http链接  
          urls: url_image // 需要预览的图片http链接列表  
        })
      }
    },
    //播放视频
    playvideo(e) {
      if (e.target.dataset.pic_url[0].poster) {
        this.setData({
          video_url: e.target.dataset.pic_url[0].url,
        })
        var start_id = this.selectComponent("#start_id");  //全屏播放获取video的节点
        start_id.playvideo(e.target.dataset.pic_url[0].url);//设置全屏播放的方法，把参数传到video_play 这个页面的playvideo方法
      } else {
        this.setData({
          video_url: '',
        })
      }
    },

    //下载搭配
    downloaddapei(e) {
      var that = this;
      let type = '';//设置下载的类型是什么。image 或者是video
      if (e.target.dataset.item[0].poster) {
        type = 'video'
      } else {
        type = 'image'
      }
      let picture_url = [];
      for (let i = 0; i < e.target.dataset.item.length; i++) {
        let str = e.target.dataset.item[i].url;
        if (type == 'image') {
          picture_url.push(str.substring(0, str.lastIndexOf("~")))  //截取最后一个~符号
        } else {
          picture_url.push(str)
        }
      }
      if (e.target.dataset.isdownload == 1) {
        wx.showModal({
          title: '提示',
          content: '您已下载了该资源，是否需要再次下载',
          success: function (res) {
            if (res.confirm) {
              let myEventDetail = {
                "picture_download": picture_url,
                "content": e.target.dataset.content,
                "tuiguangkey": e.target.dataset.item[0].tuiguang_key,
                "tuiguangindex": e.target.dataset.index,
                "type": type
              } // detail对象，提供给事件监听函数
              that.triggerEvent('downloaddapei', myEventDetail) //myevent自定义名称事件，父组件中使用
            } else {
              console.log('用户点击取消');
            }
          }
        })
      } else {
        let myEventDetail = {
          "picture_download": picture_url,
          "content": e.target.dataset.content,
          "tuiguangkey": e.target.dataset.item[0].tuiguang_key,
          "tuiguangindex": e.target.dataset.index,
          "type": type
        } // detail对象，提供给事件监听函数
        that.triggerEvent('downloaddapei', myEventDetail) //myevent自定义名称事件，父组件中使用
      }
      // let myEventDetail = {
      //   "picture_download": picture_url,
      //   "content":e.target.dataset.content,
      //   "tuiguangkey": e.target.dataset.item[0].tuiguang_key,
      //   "tuiguangindex":e.target.dataset.index,
      //   "type":type
      // } // detail对象，提供给事件监听函数
      // this.triggerEvent('downloaddapei', myEventDetail) //myevent自定义名称事件，父组件中使用
    },
    copyText(e) {
      wx.setClipboardData({
        data: e.currentTarget.dataset.text,
        success: function (res) {
          wx.getClipboardData({
            success: function (res) {
              wx.showToast({
                title: '复制成功'
              })
            }
          })
        }
      })
    },
  }
})